home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Devices / ADB Key Spy 1.0.1b3 / ADBKS Driver.c < prev    next >
Encoding:
Text File  |  1997-03-07  |  3.3 KB  |  160 lines  |  [TEXT/R*ch]

  1.     //
  2.     //    from 'ADB Key Spy' • Pete Gontier • gurgle@apple.com
  3.     //    Macintosh Developer Technical Support
  4.     //    © 1995,1996 Apple Computer, Inc.
  5.     //
  6.     //    Changes:
  7.     //
  8.     //        when        who        what
  9.     //        --------------------------------------------------------------
  10.     //        970211        Quinn    removed gOpenCount, which gave the (erroneous)
  11.     //                            impression that DRVR's receive multiple
  12.     //                            open calls, which they don't
  13.     //        01/11/96    PG        copied from another project,
  14.     //                            merged with various parts of old
  15.     //                            ADB Key Spy
  16.     //
  17.  
  18. #ifndef __DEVICES__
  19. #    include <Devices.h>
  20. #endif
  21.  
  22. #ifndef __A4STUFF__
  23. #    include <A4Stuff.h>
  24. #endif
  25.  
  26. #ifndef __SETUPA4__
  27. #    include <SetUpA4.h>
  28. #endif
  29.  
  30. #include "ADBKS Common.h"
  31. #include "ADBKS csCodes.h"
  32.  
  33. static tKeyboardInfo    *gKeyboardInfoList;
  34.  
  35. static pascal Boolean IsKeyDownAnywhere (unsigned char keyCode)
  36. {
  37.     //
  38.     //    Walks list of patched keyboards searching for one with the key
  39.     //    corresponding to the specified virtual key code held down.
  40.     //    Note this routine inspects arrays of virtual key codes
  41.     //    which were built elsewhere.
  42.     //
  43.  
  44.     if (keyCode <= kMaxKeyCode)
  45.     {
  46.         tKeyboardInfo *scan = gKeyboardInfoList;
  47.         while (scan)
  48.         {
  49.             if (scan->virtualKeyMap [keyCode] > 0)
  50.                 return true;
  51.     
  52.             scan = scan->next;
  53.         }
  54.     }
  55.  
  56.     return false;
  57. }
  58.  
  59. static pascal OSErr myOpen (ParmBlkPtr, DCtlPtr)
  60. {
  61.     return noErr;
  62. }
  63.  
  64. static pascal OSErr myPrime (ParmBlkPtr, DCtlPtr)
  65. {
  66.     //
  67.     //    Silently does nothing in case somebody foolish decides to call it.
  68.     //
  69.  
  70.     return noErr;
  71. }
  72.  
  73. static pascal OSErr myControl (CntrlParamPtr pbp, DCtlPtr)
  74. {
  75.     //
  76.     //    Sets the (global) root of the list of patched keyboard records.
  77.     //    The list can only be set once to a non-nil value, presumably
  78.     //    by the extension which loaded this driver.
  79.     //
  80.  
  81.     OSErr result = controlErr;
  82.  
  83.     switch (pbp->csCode)
  84.     {
  85.         case kControlCode_SetKeyboardInfoList :
  86.  
  87. #if PARANOID
  88.             if (!gKeyboardInfoList)
  89.             {
  90. #endif
  91.                 gKeyboardInfoList = *((tKeyboardInfo **) (pbp->csParam));
  92.                 result = noErr;
  93. #if PARANOID
  94.             }
  95. #endif
  96.  
  97.             break;
  98.     }
  99.  
  100.     return result;
  101. }
  102.  
  103. static pascal OSErr myStatus (CntrlParamPtr pbp, DCtlPtr)
  104. {
  105.     //
  106.     //    Driver-level interface to IsKeyDownAnywhere.
  107.     //    On input, the first byte of csParam should be the virtual key code.
  108.     //    On output, the first byte of csParam is a boolean describing
  109.     //    whether the key is being held down.
  110.     //
  111.  
  112.     OSErr result = statusErr;
  113.  
  114.     switch (pbp->csCode)
  115.     {
  116.         case kStatusCode_IsKeyDownAnywhere :
  117.  
  118.             {
  119.                 unsigned char *csParam = (unsigned char *) pbp->csParam;
  120.                 *csParam = IsKeyDownAnywhere (*csParam);
  121.                 result = noErr;
  122.             }
  123.             break;
  124.     }
  125.  
  126.     return result;
  127. }
  128.  
  129. static pascal OSErr myClose (ParmBlkPtr, DCtlPtr)
  130. {
  131.     return noErr;
  132. }
  133.  
  134. OSErr main (ParmBlkPtr paramBlock, DCtlPtr devCtlEnt, short dispatch)
  135. {
  136.     //
  137.     //    This is just glue for the rest of the driver. Pathetic that
  138.     //    compilers must also provide glue which will only call this glue.
  139.     //    Oh well, the Device Manager works the way it works.
  140.     //
  141.  
  142.     OSErr err = noErr;
  143.  
  144.     long oldA4 = SetCurrentA4 ( );
  145.     RememberA4 ( );
  146.  
  147.     switch(dispatch)
  148.     {
  149.         case 0: err = myOpen    (paramBlock, devCtlEnt);                    break;
  150.         case 1: err = myPrime    (paramBlock, devCtlEnt);                    break;
  151.         case 2:    err = myControl    (&(paramBlock->cntrlParam), devCtlEnt);        break;
  152.         case 3: err = myStatus    (&(paramBlock->cntrlParam), devCtlEnt);        break;
  153.         case 4: err = myClose    (paramBlock, devCtlEnt);                    break;
  154.     }
  155.  
  156.     SetA4 (oldA4);
  157.  
  158.     return err;
  159. }
  160.